#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <cstdlib>

// 示例函数
void print_sigset(const char *msg, const sigset_t *set) {
    int i;

    printf("%s:\n", msg);
    for (i = 1; i < NSIG; ++i) {
        if (sigismember(set, i)) {
            printf("\tSignal %d is in the set.\n", i);
        }
    }
}

int main() {
    sigset_t my_signals;

    // 初始化信号集为空集
    if (sigemptyset(&my_signals) == -1) {
        perror("sigemptyset");
        //EXIT_FAILURE==1，返回1示程序执行表示失败
        return(1);
    }

    // 添加信号SIGINT到信号集中
    if (sigaddset(&my_signals, SIGINT) == -1) {
        perror("sigaddset");
        return(1);
    }

    // 检查SIGINT是否在信号集中
    if (sigismember(&my_signals, SIGINT) == 1) {
        printf("SIGINT is present in the signal set.\n");
    } else {
        printf("An error occurred while checking SIGINT presence.\n");
        return(1);
    }

    // 删除信号SIGQUIT
    if (sigdelset(&my_signals, SIGQUIT) == -1) {
        perror("sigdelset");
        return(1);
    }

    // 打印当前信号集的状态
    print_sigset("Current signal set:", &my_signals);

    // 将信号集设置为包含所有信号（注意：在实际场景中可能不需要这样做）
    if (sigfillset(&my_signals) == -1) {
        perror("sigfillset");
        return(1);
    }

    // 再次打印信号集状态
    print_sigset("Signal set after sigfillset:", &my_signals);

    return(0);
}